package com.thesis.manage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.mongodb.impl.AbstractMongoServiceImpl;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.DocumentInfo;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.DocumentInfoService;
import com.thesis.manage.service.ThesisService;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.AnswerStateEnum;
import com.thesis.manage.service.model.DocumentEnum;
import com.thesis.manage.service.model.ExamineEnum;
import com.thesis.manage.service.model.TimeTypeEnum;
import com.thesis.manage.service.module.bean.DefenceArrange;
import com.thesis.manage.service.module.bean.TaskBook;
import com.thesis.manage.service.module.bean.ThesisInfo;
import com.thesis.manage.service.module.vo.DocumentVO;
import com.thesis.manage.service.module.vo.TimeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author php
 * @date 2018/3/19
 */
@Service
public class DocumentInfoServiceImpl extends AbstractMongoServiceImpl<DocumentInfo> implements DocumentInfoService {

    @Resource
    private ThesisService thesisService;

    @Override
    protected Class<DocumentInfo> getEntityClass() {
        return DocumentInfo.class;
    }

    @Override
    public DocumentInfo findByStudentId(Long studentId, Integer type) {
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setStudentId(studentId);
        documentInfo.setType(type);
        return this.findByOne(documentInfo);
    }

    @Override
    public void saveTaskBook(Long studentId, String name, Long id, TaskBook taskBook, Long taskId) {
        //时间验证
        Result result = thesisService.checkOpenApplication(TimeTypeEnum.BOOK.getValue());
        TimeVO timeVO = (TimeVO) result.getData();
        if (timeVO.getType().intValue() == AnswerStateEnum.NO_OPEN.getValue()) {
            throw new CommonException("no open", "任务书未开启");
        }
        if (timeVO.getType().intValue() == AnswerStateEnum.END.getValue()) {
            throw new CommonException("end", "任务书已结束");
        }
        Assert.state(studentId != null && id != null, ErrorMessage.PARAMS_IS_NULL);
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setType(DocumentEnum.TASK_BOOK.getValue());
        documentInfo.setCheck(YesOrNoEnum.YES.getValue());
        documentInfo.setDate(new Date());
        documentInfo.setStudentId(studentId);
        documentInfo.setDetail(JSONObject.toJSONString(taskBook));
        documentInfo.setTeacherId(id);
        documentInfo.setTeacherName(name);
        if (taskId == null) {
            this.insert(documentInfo);
        } else {
            documentInfo.setId(taskId);
            this.update(documentInfo);
        }
    }


    @Override
    public void saveDocument(String string, Integer type, Long studentId) {
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setDetail(string);
        documentInfo.setStudentId(studentId);
        documentInfo.setCheck(ExamineEnum.TEACHER_EXAMINEING.getValue());
        documentInfo.setType(type);
        documentInfo.setStatus(YesOrNoEnum.YES.getValue());

        DocumentInfo query = new DocumentInfo();
        query.setStudentId(studentId);
        query.setCheck(ExamineEnum.TEACHER_EXAMINEING.getValue());
        query.setType(type);
        DocumentInfo one = this.findByOne(query);
        if (one != null) {
            documentInfo.setId(one.getId());
            this.update(documentInfo);
        } else {
            query = new DocumentInfo();
            query.setStudentId(studentId);
            query.setType(type);
            List<DocumentInfo> documentInfos = this.find(query);
            if (CollectionUtils.isNotEmpty(documentInfos)) {
                for (DocumentInfo doc : documentInfos) {
                    doc.setStatus(YesOrNoEnum.NO.getValue());
                    this.update(doc);
                }
            }
            this.insert(documentInfo);
        }
    }

    @Override
    public List<DocumentInfo> findOpenReportByStudentId(Long studentId, Integer type) {
        List<DocumentInfo> result = new ArrayList<>();
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setStudentId(studentId);
        documentInfo.setStatus(YesOrNoEnum.YES.getValue());
        documentInfo.setType(type);
        //查询最新的文档
        DocumentInfo one = this.findByOne(documentInfo);
        if (one != null) {
            result.add(one);
            documentInfo = new DocumentInfo();
            documentInfo.setDocumentId(one.getId());
            //查询相应的评阅文档
            DocumentInfo info = this.findByOne(documentInfo);
            if (info != null) {
                result.add(info);
            }
        }
        return result;
    }

    @Override
    public void reviewDocument(String oldReport, String newReport, Integer status, Long id, Long teacherId, String teacherName) {
        Assert.notNull(id, ErrorMessage.PARAMS_IS_NULL);
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setId(id);
        documentInfo.setTeacherId(teacherId);
        documentInfo.setTeacherName(teacherName);
        documentInfo.setDate(new Date());
        if (status == YesOrNoEnum.YES.getValue().intValue()) {
            documentInfo.setCheck(ExamineEnum.EXAMINE_SUCCESS.getValue());
            this.update(documentInfo);
        } else {
            documentInfo.setDetail(oldReport);
            documentInfo.setCheck(ExamineEnum.EXAMINE_FAILED.getValue());
            this.update(documentInfo);
            documentInfo = new DocumentInfo();
            documentInfo.setDocumentId(id);
            documentInfo.setDetail(newReport);
            this.insert(documentInfo);
        }
    }

    @Override
    public List<DocumentVO> findOldDocumentByStudentId(Long studentId, Integer type) {
        List<DocumentVO> result = null;
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setStudentId(studentId);
        documentInfo.setType(type);
        documentInfo.setStatus(YesOrNoEnum.NO.getValue());
        List<DocumentInfo> documentInfos = this.find(documentInfo);
        if (CollectionUtils.isNotEmpty(documentInfos)) {
            result = new ArrayList<>();
            DocumentVO documentVO;
            for (DocumentInfo doc : documentInfos) {
                documentVO = new DocumentVO();
                documentVO.setDate(doc.getDate());
                documentVO.setDocumentId(doc.getId());
                documentVO.setPerson(doc.getTeacherName());
                result.add(documentVO);
            }
        }
        return result;
    }

    @Override
    public List<DocumentInfo> findOldDocumentById(Long id, Integer type) {
        List<DocumentInfo> result = new ArrayList<>();
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setId(id);
        documentInfo.setType(type);
        //查询最新的文档
        DocumentInfo one = this.findByOne(documentInfo);
        if (one != null) {
            result.add(one);
            documentInfo = new DocumentInfo();
            documentInfo.setDocumentId(one.getId());
            //查询相应的评阅文档
            DocumentInfo info = this.findByOne(documentInfo);
            result.add(info);
        } else {
            throw new CommonException("document not exist", "请联系相关人员");
        }
        return result;
    }

    @Override
    public List<ThesisInfo> findThesisInfos(Long id) {
        List<ThesisInfo> result = null;
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setStudentId(id);
        documentInfo.setType(DocumentEnum.THESIS_INFO.getValue());
        List<DocumentInfo> documentInfos = this.find(documentInfo);
        if (CollectionUtils.isNotEmpty(documentInfos)) {
            result = new ArrayList<>();
            for (DocumentInfo doc : documentInfos) {
                String detail = doc.getDetail();
                ThesisInfo thesisInfo = JSONObject.parseObject(detail, ThesisInfo.class);
                thesisInfo.setStatus(ExamineEnum.getName(doc.getCheck()));
                thesisInfo.setId(doc.getId());
                doc.setDetail(JSONObject.toJSONString(thesisInfo));
                result.add(thesisInfo);
            }
        }
        return result;
    }

    @Override
    public void reviewThesisInfo(Long id, Long id1, String name, Integer status, String message) {
        Assert.notNull(id, ErrorMessage.PARAMS_IS_NULL);
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setId(id);
        documentInfo.setTeacherId(id1);
        documentInfo.setTeacherName(name);
        documentInfo.setDate(new Date());
        if (status == YesOrNoEnum.YES.getValue().intValue()) {
            documentInfo.setCheck(ExamineEnum.EXAMINE_SUCCESS.getValue());
            this.update(documentInfo);
        } else {
            documentInfo.setCheck(ExamineEnum.EXAMINE_FAILED.getValue());
            this.update(documentInfo);
            documentInfo = new DocumentInfo();
            documentInfo.setDocumentId(id);
            documentInfo.setDetail(message);
            this.insert(documentInfo);
        }

    }

    @Override
    public DocumentInfo findDefenceArrange(String collegeName) {
        Assert.notNull(collegeName, ErrorMessage.PARAMS_IS_NULL);
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setType(DocumentEnum.DEFENCE_ARRANGE.getValue());
        documentInfo.setCollegeName(collegeName);
        return this.findByOne(documentInfo);
    }

    @Override
    public void saveDefenceArrange(Teacher teacher, String url) {
        Assert.notNull(teacher, ErrorMessage.USER_NO_LOGIN);
        Assert.notNull(url, ErrorMessage.PARAMS_IS_NULL);
        DocumentInfo documentInfo = new DocumentInfo();
        documentInfo.setCollegeName(teacher.getCollegeName());
        documentInfo.setType(DocumentEnum.DEFENCE_ARRANGE.getValue());
        DocumentInfo one = this.findByOne(documentInfo);
        if (one != null) {
            this.deleteById(one.getId());
        }
        documentInfo.setTeacherId(teacher.getId());
        documentInfo.setTeacherName(teacher.getName());
        DefenceArrange defenceArrange = new DefenceArrange();
        defenceArrange.setUrl(url);
        documentInfo.setDetail(JSONObject.toJSONString(defenceArrange));
        documentInfo.setDate(new Date());
        this.insert(documentInfo);
    }

}
